/*
 * @Author: liu
 * @Date: 2024-02-19 22:26:28
 * @LastEditors: liu
 * @LastEditTime: 2024-03-07 00:44:56
 * @FilePath: \koa-pro\app\controller\IndexController.ts
 * @Description:
 * 天道酬勤
 */

import { Context } from 'koa'
import logger from '../logger'
import adminService from '../service/AdminService'
import response from '../../utils/response'
import { IAdmin } from '../validator/type'
import { UserValidationException } from '../exception/HttpException'
import validate from '../validator/validator'
import { Rules } from 'async-validator'

class IndexController {
  async index(ctx: Context) {
    //logger.error('msg', 'msg')
    // const admin = await adminService.getAdmin()

    const rules: Rules = {
      name: [
        {
          type: 'string',
          required: true,
          message: '用户名不能为空'
        }
      ]
    }

    const { data, error } = await validate<IAdmin>(ctx, rules)

    if (error !== null) {
      return response.error(ctx, error)
    }
  }

  async test(ctx: Context) {}

  async testValidate(ctx: Context) {
    console.log(process.env['ALIBABA_CLOUD_ACCESS_KEY_ID'], '11')
    console.log(process.env['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
    console.log(process.argv.slice(2))

    const rules: Rules = {
      name: [
        {
          type: 'string',
          required: true,
          message: '用户名不能为空'
        },
        {
          pattern: /^\w{6,20}$/,
          message: '用户名必须由6-20位数字/字母/下划线组成'
        }
      ],
      password: [
        {
          type: 'string',
          required: true,
          message: '密码不可以为空！'
        },
        {
          min: 6,
          max: 20,
          message: '密码应大于6位小于20位！'
        }
      ]
    }

    const { data, error } = await validate<IAdmin>(ctx, rules)

    if (error !== null) {
      throw new UserValidationException(error, error.message)
    } else {
      response.success(ctx, {}, '校验通过')
    }
  }
}

export default new IndexController()
